﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DaInterFace;
using DaInterFace.PDA;
using DaInterFace.PDA.P02;

namespace P02
{
    /// <summary>
    /// 读取质检磁卡信息
    /// </summary>
    public class A04 : IPDAChild
    {
        public void Code(IPDA handler, CodeResult CR, PDACodeUser user, String parameter)
        {
            P02A04P P = (P02A04P)handler.XML.Deserialize(typeof(P02A04P), parameter);

            P02A04R R = new P02A04R();
            
            try
            {
                SqlParameter[] sps = new SqlParameter[1];
                sps[0] = new SqlParameter("@Serial", SqlDbType.NVarChar, 50);
                sps[0].Value = P.Serical;

                Recordset rs0 = handler.Data.ExeBase("Select A.FBase BaseId,A.FNumber CardNumber,A.FText2 BillID,A.FComboBox FType,B.FNumber BaseNo From KEW_ICCardEntry A LEFT JOIN KEW_DataBaseEntry B ON A.FBase=B.FID Where A.FText=@Serial", sps);
                if (!rs0.Enable)
                {
                    CR.Result = EnumResult.Error;
                    CR.Message = "磁卡数据库错误";
                    return;
                }
                if (rs0.Data.Tables.Count != 1 || rs0.Data.Tables[0].Rows.Count != 1)
                {
                    CR.Result = EnumResult.Error;
                    CR.Message = "没有该磁卡的信息";
                    return;
                }
                if (rs0.Data.Tables[0].Rows[0]["FType"].ToString() != "1")
                {
                    CR.Result = EnumResult.Error;
                    CR.Message = "不是有效的销售磁卡";
                    return;
                }

                DataRow row0 = rs0.Data.Tables[0].Rows[0];

                sps = new SqlParameter[2];
                sps[0] = new SqlParameter("@BaseNo", SqlDbType.NVarChar, 50);
                sps[0].Value = row0["BaseNo"].ToString();
                sps[1] = new SqlParameter("@BillID", SqlDbType.NVarChar, 50);
                sps[1].Value = row0["BillID"].ToString();

                StringBuilder sqlSearch = new StringBuilder();
                sqlSearch.Append("SELECT @BaseNo BaseId,A.FID BillID,A.FBillNo,A.FText CarNo,IsNull(A.FDecimal17,0) RedQty,IsNull(A.FComboBox4,0) RedStyle,IsNull(A.FText10,'') RedReason,IsNull(A.FComboBox2,'') MType,IsNull(B.FEntryID,0) EntryID,IsNull(C.FID,0) SampID,IsNull(C.FBillNo,'') SampNo,C.FCheckBox IsOver\r\n");
                sqlSearch.Append("FROM KEW_PurchInfo A LEFT JOIN KEW_PurchSamplingEntry B ON A.FID=B.FID_SRC\r\n");
                sqlSearch.Append("LEFT JOIN KEW_PurchSampling C ON B.FID=C.FID WHERE A.FID=@BillID\r\n");
                sqlSearch.Append("Select A.FID,A.FBillNo BillNo,A.FInteger FCount,A.FTime BuildTime,A.FText1 OrderNo,B.FName SupplierName,C.FName ItemName From KEW_PurchSampling A \r\n");
                sqlSearch.Append("Left Join t_Supplier B On A.FBase=B.FItemID \r\n");
                sqlSearch.Append("Left Join KEW_PurchItem C On A.FBase1=C.FID \r\n");
                sqlSearch.Append("Where A.FCheckBox=0 And A.FID_SRC1=(SELECT FID_SRC FROM KEW_PurchInfoEntry WHERE FIndex=1 AND FID=@BillID)");

                Recordset rs1 = handler.Data.ExeProcedure(row0["BaseNo"].ToString(), sqlSearch.ToString(), sps);
                if (!rs1.Enable)
                {
                    CR.Result = EnumResult.Error;
                    CR.Message = "读取磁卡绑定信息执行错误";
                    return;
                }
                if (rs1.Data.Tables.Count != 2 || rs1.Data.Tables[0].Rows.Count != 1)
                {
                    CR.Result = EnumResult.Error;
                    CR.Message = "读取磁卡绑定信息错误";
                    return;
                }
                if (Common.CBool(rs1.Data.Tables[0].Rows[0]["IsOver"].ToString()))
                {
                    CR.Result = EnumResult.Error;
                    CR.Message = String.Format("采样单{0}已经结束采样，不能修改！", rs1.Data.Tables[0].Rows[0]["SampNo"].ToString());
                    return;
                }

                R.Table1 = rs0.Data.Tables[0];
                R.Table2 = rs1.Data.Tables[0];
                R.Table3 = rs1.Data.Tables[1];

                R.Table1.TableName = "Table1";
                R.Table2.TableName = "Table2";
                R.Table3.TableName = "Table3";

                CR.Content = handler.XML.Serializer(typeof(P02A04R), R);
                CR.Message = String.Format("操作员{0}获取账套{1}读取质检磁卡信息：{2}", user.UserName, row0["BillID"].ToString(), P.Serical);
            }
            catch (Exception ex)
            {
                CR.Result = EnumResult.Error;
                CR.Message = String.Format("发生错误:{0}", ex.Message);
            }
        }
    }


}
